import torch.nn as nn

# Define the model
class SimpleMLP(nn.Module):
    def __init__(self, res_connect=True):
        super(SimpleMLP, self).__init__()
        self.res_connect = res_connect

        self.fc1 = nn.Linear(28 * 28, 128)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 64)
        self.fc4 = nn.Linear(64, 64)
        self.fc5 = nn.Linear(64, 64)
        self.fc_out = nn.Linear(64, 10)

        # self._init_weight()

    def forward(self, x):
        x = x.view(-1, 28 * 28)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        x = self.relu(x)

        if self.res_connect:
            residual = x.clone()
        
        x = self.fc3(x)
        # x = self.relu(x)
        x = self.fc4(x)
        x = self.relu(x)

        if self.res_connect:
            x = x + residual

        x = self.fc5(x)
        x = self.relu(x)

        x = self.fc_out(x)
        return x

    def _init_weight(self, method='xavier'):
        if method == 'xavier':
            for m in self.modules():
                if isinstance(m, nn.Linear):
                    nn.init.xavier_uniform_(m.weight)
                    if m.bias is not None:
                        nn.init.constant_(m.bias, 0)

        elif method == 'kaiming':
            for m in self.modules():
                if isinstance(m, nn.Linear):
                    nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                    if m.bias is not None:
                        nn.init.constant_(m.bias, 0)
